    <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?><!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="Bookmark" href="/favicon.ico" >
    <link rel="Shortcut Icon" href="/favicon.ico" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="/public/js/html5shiv.js"></script>
    <script type="text/javascript" src="/public/js/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/Hui-iconfont/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/h-ui.admin/css/style.css" />
    <link rel="stylesheet" type="text/css" href="/public/plugins/laydate/skins/default/laydate.css"/>
    <!--[if IE 6]>
    <script type="text/javascript" src="/public/js/DD_belatedPNG_0.0.8a-min.js" ></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <title>快方管理后台</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <style type="text/css">
        .date{
            width: 100px;
        }
        .table .show_count{
            text-align: right;
            padding-right: 10px;
        }
        .table{
            user-select:none;/**禁止选中文字*/ 
        }
        .search{
            width: 250px;
        }
        .title{
            padding-left: 20px;
        }
        .addCategory,.modCategory{
            display: none;
        }
        #productList tbody tr:hover{
            cursor:pointer;
        }
        .upload_file_name{
            margin-top: 10px;
            padding-left: 10px;
            font-size: 14px;
        }
        .upload_file{
            margin: 10px 0px;
        }
        .chn_list .btn{
            margin-right:10px; 
        }
        .reviewPrice{
            display: none;

        }
    </style>
</head>
<body class="big-page">
<div class="Hui-article" style="top:0;">
    <h3 class="title">药店列表<small>&emsp;<?=$city_name?></small></h3>
    <h5 class="title">
        <?php
        foreach($channel as $r){
        ?>
            <?=$r['name']?> [ <span class='c-blue'><?=$r['sync_cnt']?></span> ]&emsp;
        <?php
        }
        ?>
    </h5>
    <article class="cl pd-20" style="padding-top: 0px;">
        <div class="control">
            
            <a class="go_back btn-primary btn radius">返回</a>
        </div>

        <div class="mt-10">
            <table class="table table-border table-bordered table-bg table-hover table-sort table-responsive" id="productList">
                <thead>
                    <tr>
                        <td colspan="2">
                                <input type="text" class="search input-text radius" name="search">
                                <a href="#" class="select_all btn btn-success radius">全选</a>
                        </td> 
                        <td colspan="1" class="show_count"></td> 
                    </tr>
                    <tr class="text-c">
                        <th>#</th>
                        <th>药店id</th>
                        <th>药店名称</th>
                    </tr>
                </thead>
                
                <tbody>

                </tbody>
            </table>
        </div>
        <h3 class="title" style="padding-left: 0px;">价格信息<small></h3>
        <div class="upload_file">
            <form class="upload_price_form">
                <table class="table table-border table-bordered table-bg table-hover table-sort table-responsive">
                    <tr>
                        <td>渠道</td>
                        <td class="chn_list">

                        </td>
                    </tr>
                    <tr>
                        <td>文件</td>
                        <td>
                            <input type="button" value="上传文件" class="btn radius" name="upload_file_btn">
                            <input type="file" id="price_file" accept="text/csv" style="display: none;" />
                            <div class="upload_file_name c-blue">&nbsp;</div>
                            
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align: center;">
                            <a class="sync_price btn btn-success radius">价格预览</a>
                            <input name="upload_chn_id" type="hidden" />
                            <input name="upload_store_id" type="hidden" />
                            <input name="upload_file_content" type="hidden"/>
                        </td>
                    </tr>
                </table>
            </form>
        </div>

        <div class="reviewPrice">
            <h3 class="title" style="padding-left: 0px;">价格预览<small></h3>
            <div class="mt-10">
                <table class="table table-border table-bordered table-bg table-hover table-sort table-responsive" id="productPriceList">
                    <thead>
                        <tr>
                            <td colspan="7">
                                <input type="text" class="search input-text radius" name="price_search">
                                <a href="#" class="commit_price btn btn-success radius">提交价格</a>
                            </td> 
                            <td colspan="1" class="price_show_count"></td> 
                        </tr>
                        <tr class="text-c">
                            <th>#</th>
                            <th>KFID</th>
                            <th>渠道名称</th>
                            <th>药店名称</th>
                            <th>药品名称</th>
                            <th>药品规格</th>
                            <th>生产厂家</th>
                            <th>当前价格</th>
                            <th>更新价格</th>
                        </tr>
                    </thead>
                    
                    <tbody>

                    </tbody>
                </table>
            </div>
        </div>
    </article>
    <input type="hidden" name="store_id" value="<?=$_GET['store_id']?>">
    <input type="hidden" name="chn_id" value="<?=$_GET['chn_id']?>">
</div>

<script type="text/javascript" src="/public/js/jquery.min.js"></script>
<script type="text/javascript" src="/public/plugins/layer/layer.js"></script>
<script type="text/javascript" src="/public/plugins/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="/public/plugins/h-ui.admin/js/H-ui.admin.js"></script>
<script type="text/javascript" src="/public/plugins/jquery.contextmenu/jquery.contextmenu.r2.js"></script>
<script type="text/javascript" src="/public/plugins/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/public/plugins/laydate/laydate.js"></script>
<script type="text/javascript">

    var mouseDownFlag = false;


    var loading = layer.msg('处理中', {
      icon: 16
      ,shade: 0.5
    });
    $.ajax({
        url: window.location.href,
        type: "POST",
        dataType: "json",
        success: function(res){
            layer.close(loading);
            var html = "";
            var i = 1; 
            for(k in res.data){
                var data = res.data[k];

                html += "<tr class='hand' data-store_id='" + data.ID + "'>";
                html += "<td>" + i + "</td>";
                html += "<td>" + data.ID + "</td>";
                html += "<td>" + data.Caption + "</td>";
                html += "</tr>";

                i++;
            }
            $(".show_count").html("共&emsp;" + (i-1) + "&emsp;条记录")
            $("#productList tbody").html(html)
        }
    })


    $.ajax({
        url: "/ChannelProduct/getChnList",
        type: "POST",
        dataType: "json",
        success: function(res){
            var html = "";

            for(k in res.data){
                var data = res.data[k];
                html += "<span href='#' class='chn_id btn radius' data-chn_id='" + data.id + "'>" + data.name + "</span>"
            }

            $(".chn_list").html(html)
        }
    })
    $("input[name='search']").bind("keyup",function() {
        var text = $(this).val();

        $("#productList tbody tr").hide();

        if(text.length > 0){
            $("#productList tbody tr").each(function(){
                var product_name = $("td:eq(2)", this).text();
                var company_name = $("td:eq(5)", this).text();
                var product_id = $("td:eq(1)", this).text();

                if(product_name.indexOf(text) > -1){
                    $(this).show();
                }

                if(company_name.indexOf(text) > -1){
                    $(this).show();
                }

                if(product_id == text){
                    $(this).show();
                }
            })
        }else{
            $("#productList tbody tr").show();
        }
    })  

    $("#productList tbody tr").onselectstart = function(){return false;}


    $(document).on("click", "#productList tbody tr", function(){
        var has = $(this).hasClass("success");

        var store_id = new Array();
        if(has){
            $(this).removeClass("success");
        }else{
            $(this).addClass("success");
        }

        $("#productList tbody tr").each(function(i, ele){
            var has = $(ele).hasClass("success");

            if(has){
                store_id.push($(ele).data("store_id"));
            }
        })

        $("input[name='upload_store_id']").val(store_id.join("|"));
    })

    $(document).on("click", ".chn_id", function(){
        var has = $(this).hasClass("btn-secondary");

        var chn_id = new Array();

        if(has){
            $(this).removeClass("btn-secondary");
        }else{
            $(this).addClass("btn-secondary");
        }

        $(".chn_id").each(function(i, ele){
            var has = $(ele).hasClass("btn-secondary");

            if(has){
                chn_id.push($(ele).data("chn_id"));
            }
        })

        $("input[name='upload_chn_id']").val(chn_id.join("|"));
    })

    $(document).on("mouseover", "#productList tbody tr", function(){
        var has = $(this).hasClass("success");

        if(mouseDownFlag){
            $(this).click();
        }
    })

    $(".select_all").bind("click", function(){
        var hasClass = $("#productList tbody tr:eq(0)").hasClass("success");
        var store_id = new Array();

        if(hasClass){
            $("#productList tbody tr").removeClass("success");
        }else{
            $("#productList tbody tr").addClass("success");
        }

        $("#productList tbody tr").each(function(i, ele){
            var has = $(ele).hasClass("success");

            if(has){
                store_id.push($(ele).data("store_id"));
            }
        })

        $("input[name='upload_store_id']").val(store_id.join("|"));
    })

    $(".upload_price").bind("click", function(){

    })

    $("input[name='upload_file_btn']").bind("click", function(){
        $("#price_file").click();
    })

    String.prototype.replaceAll = function(s1,s2){ 
        return this.replace(new RegExp(s1,"gm"),s2); 
    } 

    $(document).on("change", "#price_file", function(){
        var files = $(this)[0].files;
        var file_name = files[0].name;

        $(".upload_file_name").html(file_name)
        if( typeof(FileReader) !== 'undefined' ){    //H5
            var reader = new FileReader();
            reader.readAsText( files[0] );            //以文本格式读取
            reader.onload = function(evt){
                var data = evt.target.result;        //读到的数据
                $("input[name='upload_file_content']").val(data.replaceAll("\n", "|"));
            }
        }else{
            alert("IE9及以下浏览器不支持，请使用Chrome或Firefox浏览器");
        }
    })

    $(".sync_price").bind("click", function(){
        var data = {
            "chn_id" : $("input[name='upload_chn_id']").val(),
            "store_id" : $("input[name='upload_store_id']").val(),
            "product_price" : $("input[name='upload_file_content']").val(),
        };

        if(data.chn_id.length < 1){
            layer.alert("你还没有选择渠道");
            return false;
        }

        if(data.store_id.length < 1){
            layer.alert("你还没有选择药店");
            return false;
        }

        if(data.product_price.length < 1){
            layer.alert("你还没有上传文件");
            return false;
        }

        var loading = layer.msg('处理中', {
          icon: 16
          ,shade: 0.5
        });
        $(".price_show_count").html("共&emsp;0&emsp;条记录")
        $("#productPriceList tbody").html("")
        $.ajax({
            url: "/ChannelProduct/syncPrice",
            type: "POST",
            data: data,
            dataType: "json",
            success: function(res){
                layer.close(loading)
                var success = "";
                var error = "";
                var i = 1;
                $(".reviewPrice").show();
                for(k in res.data){
                    var data = res.data[k];
                    var tr_class = data.other_product_price == data.new_other_price ? "danger" : "success";
                    var new_price = (data.new_other_price/ 100);
                    new_price = new_price.toFixed(2);
                    var price = (data.other_product_price/100);
                    price = price.toFixed(2);
                    var html = "";
                    html += "<tr class='" + tr_class + "'>";
                    html += "<td>" + i + "</td>";
                    html += "<td>" + data.fk_product_id + "</td>";
                    html += "<td>" + data.channel_name + "</td>";
                    html += "<td>" + data.store_name + "</td>";
                    html += "<td>" + data.product_info.Caption + "</td>";
                    html += "<td>" + data.product_info.ModeSize + "</td>";
                    html += "<td>" + data.product_info.CompanyName + "</td>";
                    html += "<td>" + price + "</td>";
                    html += "<td>" + new_price + "</td>";
                    html += "</tr>";

                    

                    if(data.other_product_price == data.new_other_price){
                        error += html;
                    }else{
                        success += html;
                    }
                    i++;
                }

                $(".price_show_count").html("共&emsp;" + (i-1) + "&emsp;条记录")
                $("#productPriceList tbody").html(success + error)
            }
        })
    })


    $(".commit_price").bind("click", function(){
        var data = {
            "chn_id" : $("input[name='upload_chn_id']").val(),
            "store_id" : $("input[name='upload_store_id']").val(),
            "product_price" : $("input[name='upload_file_content']").val(),
        };

        if(data.chn_id.length < 1){
            layer.alert("你还没有选择渠道");
            return false;
        }

        if(data.store_id.length < 1){
            layer.alert("你还没有选择药店");
            return false;
        }

        if(data.product_price.length < 1){
            layer.alert("你还没有上传文件");
            return false;
        }

        var loading = layer.msg('处理中', {
          icon: 16
          ,shade: 0.5
        });

        $.ajax({
            url: "/ChannelProduct/commitSyncPrice",
            type: "POST",
            data: data,
            dataType: "json",
            success: function(res){
                layer.close(loading);
                window.location.reload();
            }
        })
    })

    $("input[name='price_search']").bind("keyup",function() {
        var text = $(this).val();

        $("#productPriceList tbody tr").hide();

        if(text.length > 0){
            $("#productPriceList tbody tr").each(function(){
                var product_name = $("td:eq(4)", this).text();
                var company_name = $("td:eq(6)", this).text();
                var channel_name = $("td:eq(2)", this).text();
                var product_id = $("td:eq(1)", this).text();

                if(product_name.indexOf(text) > -1){
                    $(this).show();
                }
                
                if(channel_name.indexOf(text) > -1){
                    $(this).show();
                }

                if(company_name.indexOf(text) > -1){
                    $(this).show();
                }

                if(product_id.indexOf(text) > -1){
                    $(this).show();
                }
            })
        }else{
            $("#productPriceList tbody tr").show();
        }
    })

    $(document).mousedown(function(){
        mouseDownFlag = true;
    })

    $(document).mouseup(function(){
        mouseDownFlag = false;
    })

    $(".go_back").bind("click", function(){
        window.history.back();
    })
</script>
</body>
</html>
